package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.config.AliyunOSSUtils;
import com.ruoyi.system.domain.Gift;
import com.ruoyi.system.service.IGiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 礼物管理Controller
 * 
 * @author ruoyi
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/gift")
public class GiftController extends BaseController
{
    @Autowired
    private IGiftService giftService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    /**
     * 查询礼物管理列表
     */
    @RequiresPermissions("gift:gift:list")
    @GetMapping("/list")
    public TableDataInfo list(Gift gift)
    {
        startPage();
        List<Gift> list = giftService.selectGiftList(gift);
        return getDataTable(list);
    }

    /**
     * 导出礼物管理列表
     */
    @RequiresPermissions("gift:gift:export")
    @Log(title = "礼物管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Gift gift)
    {
        List<Gift> list = giftService.selectGiftList(gift);
        ExcelUtil<Gift> util = new ExcelUtil<Gift>(Gift.class);
        util.exportExcel(response, list, "礼物管理数据");
    }

    /**
     * 获取礼物管理详细信息
     */
    @RequiresPermissions("gift:gift:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(giftService.selectGiftById(id));
    }

    /**
     * 新增礼物管理
     */
    @RequiresPermissions("gift:gift:add")
    @Log(title = "礼物管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Gift gift)
    {
        return toAjax(giftService.insertGift(gift));
    }

    /**
     * 修改礼物管理
     */
    @RequiresPermissions("gift:gift:edit")
    @Log(title = "礼物管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Gift gift)
    {
        return toAjax(giftService.updateGift(gift));
    }

    /**
     * 删除礼物管理
     */
    @RequiresPermissions("gift:gift:remove")
    @Log(title = "礼物管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(giftService.deleteGiftByIds(ids));
    }

    //图片上传下载
    @RequestMapping("fileUpload")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String s = aliyunOSSUtils.putObject(inputStream, originalFilename);
        return s;
    }
}
